package com.atguigu.java;

import java.util.Objects;

/**
 * @author yihu
 * @create 2021-03-04 16:04
 * @email:1057202547@qq.com
 * @dec:
 */
public class User implements  Comparable{

    private  String name;
    private  int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("user equals");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
       // return Objects.hash(name, age);
        int result = name!=null ? name.hashCode() : 0;
        result =result * 31 + age;
        return  result;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof  User){
            User user = (User) o;
            if(this.age > user.age){
                return  1;
            }else  if(this.age < user.age){
                return  -1;
            }else {
                return  -(this.name.compareTo(user.name));
            }
        }
        throw  new RuntimeException("传入的数据类型不一致");
    }
}
